
$(document).ready(function(){
    ajax();
})

var hotelId="";
var content_url = window.location.href;
content_url = content_url.split("?");
Hotel_ID = content_url[ content_url.length-18 ].split("=")[ content_url[ content_url.length-18 ].split("=").length-1 ];
hotelId=Hotel_ID;
// 封装ajax请求 动态添加选择的酒店数据
function ajax(){
    var content_url = window.location.href;
    content_url = content_url.split("?");
    console.log( content_url );
    $.ajax({
        // url:url+"v1/hotel/detail",
        url:"php/jiudian.js",
        method:"get",
        dataType:"json",
        data:{
            hotelId:hotelId
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        console.log(data);

        if(data.code="success"){

            // 获取上传到地址栏的信息
            // 酒店图片
            var Hotel_picture=content_url[ content_url.length-1 ].split("=")[ content_url[ content_url.length-1 ].split("=").length-1 ];
            Hotel_picture=url_file+Hotel_picture;
            $(".hotel_img img").attr("src",Hotel_picture);
            // 退房星期
            var Check_out_week=content_url[ content_url.length-2 ].split("=")[ content_url[ content_url.length-2 ].split("=").length-1 ];
            Check_out_week  =  decodeURI( Check_out_week );
            // 入住星期
            var Check_week=content_url[ content_url.length-3 ].split("=")[ content_url[ content_url.length-3 ].split("=").length-1 ];
            Check_week  =  decodeURI( Check_week );
            // 退房时间戳
            var Check_out_data_time = content_url[ content_url.length-4 ].split("=")[ content_url[ content_url.length-4 ].split("=").length-1 ];
            // 入住时间戳
            var Check_data_data_time=content_url[ content_url.length-5 ].split("=")[ content_url[ content_url.length-5 ].split("=").length-1 ];
            // 判断住几晚
            var night = Check_out_data_time - Check_data_data_time;
            night =  Math.ceil(night/86400000) ;
            $(".totalTime").html( "共"+ night +"晚" );
            // 退房时间
            var Check_out_time = content_url[ content_url.length-6 ].split("=")[ content_url[ content_url.length-6 ].split("=").length-1 ];
            Check_out_time = decodeURI( Check_out_time );
            $("#leave_time").val(Check_out_time );
            // 入住时间
            var Check_time = content_url[ content_url.length-7 ].split("=")[ content_url[ content_url.length-7 ].split("=").length-1 ];
            Check_time = decodeURI( Check_time );
            $("#enter_time").val(Check_time);
            // 酒店电话
            var HotelPhone = content_url[ content_url.length-8 ].split("=")[ content_url[ content_url.length-8 ].split("=").length-1 ];
            HotelPhone = decodeURI( HotelPhone );
            HotelPhone = HotelPhone.split("电话")[HotelPhone.split("电话").length-1];
            $(".hotel_hotline span").html(HotelPhone);
            // 早餐
            var breakfast = content_url[ content_url.length-9 ].split("=")[ content_url[ content_url.length-9 ].split("=").length-1 ];
            breakfast = decodeURI( breakfast );
            $(".breakfast span").html(breakfast);
            // wifi
            var WIFI = content_url[ content_url.length-10 ].split("=")[ content_url[ content_url.length-10 ].split("=").length-1 ];
            WIFI = decodeURI( WIFI );
            $(".internet span").html(WIFI);
            // 房间面积
            var RoomArea = content_url[ content_url.length-11 ].split("=")[ content_url[ content_url.length-11 ].split("=").length-1 ];
            RoomArea = decodeURI( RoomArea );
            $(".bedArea span").html(RoomArea);
            // 床型
            var BedType = content_url[ content_url.length-12 ].split("=")[ content_url[ content_url.length-12 ].split("=").length-1 ];
            BedType = decodeURI( BedType );
            $(".bedType span").html(BedType);
            // 酒店名称
            var HotelName = content_url[ content_url.length-13 ].split("=")[ content_url[ content_url.length-13 ].split("=").length-1 ];
            HotelName = decodeURI( HotelName );
            $(".hotel_summarize h4").html(HotelName);
            // 酒店位置
            var position = content_url[ content_url.length-14 ].split("=")[ content_url[ content_url.length-14 ].split("=").length-1 ];
            position = decodeURI( position );
            $(".hotel_address").html(position);
            // 房间价格
            var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
            var room_money = RoomPrice*Math.ceil( (Check_out_data_time-Check_data_data_time)/86400000);
             // 房间数量
            var room_count  = content_url[ content_url.length-17 ].split("=")[ content_url[ content_url.length-17 ].split("=").length-1 ];
             $(".total_num").val(1);
             // $(".room_count").html("剩余"+ room_count +"间");
             // if( room_count == 1 ){
             //     $(".reduce").attr("disabled",true);
             //     $(".reduce").css("background",'url("images/icon_plus_2.png") 9px 9px no-repeat');
             // }
            
            // 计算房价
            var room_price = (room_money*room_count*night);
            console.log("总计房价"+room_price);
            $(".fontred").html(room_price);

            // 房间名称
            var RoomName = content_url[ content_url.length-16 ].split("=")[ content_url[ content_url.length-16 ].split("=").length-1 ];
            RoomName = decodeURI( RoomName );
            $(".roomtype span").html(RoomName);
            $(".bedtype").html(RoomName);
           
            // 酒店ID
            Hotel_ID = content_url[ content_url.length-18 ].split("=")[ content_url[ content_url.length-18 ].split("=").length-1 ];
            hotelId=Hotel_ID;
        }
    })
}


// 日历

lay('#version').html('-v'+ laydate.v);
        
var startTime=content_url[ content_url.length-5 ].split("=")[ content_url[ content_url.length-5 ].split("=").length-1 ];
var endTime=content_url[ content_url.length-4 ].split("=")[ content_url[ content_url.length-4 ].split("=").length-1 ];
var duringDay, startDate, endDate;
laydate.render({
    elem: '#enter_time', //指定元素
    format: 'yyyy年M月dd日',
    min: 0, //今天起
    done: function(value, date) {
        startDate = value;
        str = date.year + '/' + date.month + '/' + date.date;
        var datex = new Date(str);
        var a = new Date();//当前时间
        var b = parseInt(a.getTime());//现在时间戳
        startTime = parseInt(datex.getTime());
        if(b-startTime<86400000){
            startTime+=86400000-10000000;
        }
        console.log(startTime);

        var $totalTime = $('.totalTime');
        const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        $('#startDay').html(days[datex.getDay()]);
        console.log(value);
        var duringTime = endTime - startTime;
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            console.log( duringTime );
            console.log( duringDay );
            duringDay = Math.ceil(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }
        // console.log(duringTime);
        var room_count=$(".total_num").val();

        var content_url = window.location.href;
        content_url = content_url.split("?"); 
        //房间单价
        var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
        
        // 判断住几晚
        var $totalTime = $('.totalTime');
        var duringTime = endTime - startTime;
        console.log(endTime);
        console.log(startTime);
            if (duringTime <= 0) {
                $totalTime.html('请选择入住日期以后的日期！')
            } else {
                duringDay = parseInt(duringTime / 86400000)
                $totalTime.html('共' + duringDay + '晚');
            }
        //每次点击时计算房费总价
        var total_price=(room_count*RoomPrice*duringDay);
        console.log("duringDay"+duringDay);
        $(".fontred").html(total_price);
        console.log("房价总计"+total_price);


        return value;
    }
});
laydate.render({
    elem: '#leave_time', //指定元素
    format: 'yyyy年MM月dd日',
    min: 0, //今天起
    done: function(value, date) {
        endDate = value;
        var str = date.year + '/' + date.month + '/' + date.date;
        var $totalTime = $('.totalTime'),
            datey = new Date(str);
        const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        endTime = parseInt(datey.getTime());
        $('#endDay').html(days[datey.getDay()]);
        var duringTime = endTime - startTime;
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            duringDay = parseInt(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }

        var room_count=$(".total_num").val();

        var content_url = window.location.href;
        content_url = content_url.split("?"); 
        //房间单价
        var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
        
        // 判断住几晚
        var $totalTime = $('.totalTime');
        var duringTime = endTime - startTime;
        console.log(endTime);
        console.log(startTime);
            if (duringTime <= 0) {
                $totalTime.html('请选择入住日期以后的日期！')
            } else {
                duringDay = Math.ceil(duringTime / 86400000)
                $totalTime.html('共' + duringDay + '晚');
            }
        //每次点击时计算房费总价
        var total_price=(room_count*RoomPrice*duringDay);
        console.log("duringDay"+duringDay);
        $(".fontred").html(total_price);
        console.log("房价总计"+total_price);
    }
});
// 日历失去焦点重新计算天数和房费总价
$("#enter_time").blur(function(){
    // console.log(1);

})

// 更改房间数量并计算房费总价
$(".plus").click(function(){
    var content_url = window.location.href;
    content_url = content_url.split("?"); 
    var roomCount  = content_url[ content_url.length-17 ].split("=")[ content_url[ content_url.length-17 ].split("=").length-1 ];
    var room_count=$(".total_num").val();
    if(room_count<roomCount){
        room_count++;
        $(".total_num").val(room_count);

        //房间单价
        var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
        // 判断住几晚
        var $totalTime = $('.totalTime');
        var duringTime = endTime - startTime;
            if (duringTime <= 0) {
                $totalTime.html('请选择入住日期以后的日期！')
            } else {
                duringDay = Math.ceil(duringTime / 86400000)
                $totalTime.html('共' + duringDay + '晚');
            }
        //每次点击时计算房费总价
        var total_price=(room_count*RoomPrice*duringDay);
        $(".fontred").html(total_price);
    }
})
$(".minus").click(function(){
    var room_count=$(".total_num").val();
    if(room_count>1){    
        room_count--;
        $(".total_num").val(room_count);
        var content_url = window.location.href;
        content_url = content_url.split("?");
        var content_url = window.location.href;
        content_url = content_url.split("?"); 
        //房间单价
        var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
        
        // 判断住几晚
        var $totalTime = $('.totalTime');
        var duringTime = endTime - startTime;
            if (duringTime <= 0) {
                $totalTime.html('请选择入住日期以后的日期！')
            } else {
                duringDay = Math.ceil(duringTime / 86400000)
                $totalTime.html('共' + duringDay + '晚');
            }
        //每次点击时计算房费总价
        var total_price=(room_count*RoomPrice*duringDay);
        $(".fontred").html(total_price);        
        return;
    }
})

function compute(){
    $(".total_num").val(room_count);

    var content_url = window.location.href;
    content_url = content_url.split("?"); 
    //房间单价
    var RoomPrice = content_url[ content_url.length-15 ].split("=")[ content_url[ content_url.length-15 ].split("=").length-1 ];
    
    // 判断住几晚
    var $totalTime = $('.totalTime');
    var duringTime = endTime - startTime;
    console.log(endTime);
    console.log(startTime);
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            duringDay = parseInt(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }
}


// 生成订单——提交订单
$(".submit").click(function(){
    // var str="";
    // str+='<a class="subBtn"></a>'
    // $(".submit").append(str);
    console.log(1);
    // 房间名称
    var roomName=$(".roomtype span").html();
    // checkTime
    var duringTime =startTime+","+endTime ;
    // roomNumber
    var roomNumber=$(".total_num").val();
    // customerName
    var customerName=$(".guestName").val();
    // customerPhone
    var customerPhone=$(".guestPhone").val();
    if(customerName!="" && customerPhone!=""){
        console.log("token="+localStorage.token,"房间名="+roomName,"酒店id="+hotelId,"预定时间段="+duringTime,"房间数量="+roomNumber,"客户姓名="+customerName,"客户手机号="+customerPhone);
        $(".errorMsg").hide();
        $.ajax({
            url:url+"v1/order/create",
            method:"post",
            dataType:"json",
            data:{
                token:localStorage.token,
                roomName:roomName,
                hotelId:hotelId,
                checkTime:duringTime,
                roomNumber:roomNumber,
                customerName:customerName,
                customerPhone:customerPhone
            },
            xhrFields: {
                withCredentials: true // 允许跨域名储存和访问cookie
            }
        }).done(function(data){
            console.log(data);
            // var str="";
            if(data.code=="success"){
                // str+='<a href="pay.html?hotelPicture='+data.data.hotelPicture+'?hotelName='+data.data.hotelName+'?roomName='+data.data.roomName+'?orderAmount='+data.data.orderAmount+'?payId='+data.data._id+'"></a>'
                // $(".submit").append(str);
                window.location.href='pay.html?hotelPicture='+data.data.hotelPicture+'?hotelName='+data.data.hotelName+'?roomName='+data.data.roomName+'?orderAmount='+data.data.orderAmount+'?payId='+data.data._id;
            }else{
                $(".errorMsg").html(data.message).show();
            }
        })
    }else{
        $(".errorMsg").html("请输入入住信息").show();
    }
})

// $(".subBtn").on("click",function(){
//     console.log(1);
//     // 房间名称
//     var roomName=$(".roomtype span").html();
//     // checkTime
//     var duringTime =startTime+","+endTime ;
//     // roomNumber
//     var roomNumber=$(".total_num").val();
//     // customerName
//     var customerName=$(".guestName").val();
//     // customerPhone
//     var customerPhone=$(".guestPhone").val();
//     if(customerName!="" && customerPhone!=""){
//         console.log("token="+localStorage.token,"房间名="+roomName,"酒店id="+hotelId,"预定时间段="+duringTime,"客户姓名="+customerName,"客户手机号="+customerPhone);
//         $(".errorMsg").hide();
//         $.ajax({
//             url:url+"v1/order/create",
//             method:"post",
//             dataType:"json",
//             data:{
//                 token:localStorage.token,
//                 roomName:roomName,
//                 hotelId:hotelId,
//                 checkTime:duringTime,
//                 roomNumber:roomNumber,
//                 customerName:customerName,
//                 customerPhone:customerPhone
//             },
//             xhrFields: {
//                 withCredentials: true // 允许跨域名储存和访问cookie
//             }
//         }).done(function(data){
//             console.log(data);
//             var str="";
//             if(data.code=="success"){
//                 str+='<a href="pay.html?hotelPicture='+data.data.hotelPicture+'?hotelName='+data.data.hotelName+'?roomName='+data.data.roomName+'?orderAmount='+data.data.orderAmount+'?payId='+data.data._id+'"></a>'
//                 $(".submit").append(str);
//             }else{
//                 $(".errorMsg").html(data.message).show();
//             }
//         })
//     }else{
//         $(".errorMsg").html("请输入入住信息").show();
//     }
// })